<?php
$loader->requireOnce('includes/PaymentConstant.class.php');
class BillStorage {
    private $ticketId;
    private $startDate;
    private $endDate;
    private $patientType;
    private $examinationType;
    private $discountProgram;

    public $itemList;

    private $insuranceInfo;
    
    // ppdong 2010-04-06: paymentId (pay_payment_id) to update pay_payment record
    private $paymentId;
    
    function __construct($ticketId = '', $startDate = '', $endDate = '', $patientType = '', $examinationType = 1, $discountProgram = '') {
        $this->ticketId     =   $ticketId;
        $this->startDate    =   $startDate;
        $this->endDate      =   $endDate;
        $this->patientType  =   $patientType;
        $this->examinationType  =   $examinationType;
        $this->discountProgram  =   $discountProgram;
        $this->itemList =   array(
            PaymentConstant::ITEM_BED           => array(),
            PaymentConstant::ITEM_MATERIAL      => array(),
            PaymentConstant::ITEM_MEDICINE      => array(),
            PaymentConstant::ITEM_PARACLINIQUE  => array(),
            PaymentConstant::ITEM_SERVICE       => array()
        );
        $this->insuranceInfo    =   new InsuranceInfo();
        self::setupPaymentId();
    }

    public function getInsuranceInfo() {
    	return $this->insuranceInfo;
    }

    public function getPatientType() {
    	return $this->patientType;
    }

    public function setInsuranceInfo($obj) {
    	 $this->insuranceInfo  =   $obj;
    }

	public function getItemList($type = false) {
		if ($type == false) {
			return $this->itemList;
		}
		return $this->itemList[$type];
	}

	public function getTicketId() {
		return $this->ticketId;
	}

	public function getStartDate() {
		return $this->startDate;
	}

	public function getEndDate(){
		return $this->endDate;
	}


	public function getTotalInsuranceAmount() {
		$total    =   0;
        foreach ($this->itemList as $subList) {
            foreach ($subList as $item) {
                $total  +=  $item->getInsurancePrice();
            }
        }
        return $total;
	}

	public function getTotalDiscountAmount() {
        $total    =   0;
        foreach ($this->itemList as $subList) {
            foreach ($subList as $item) {
                $total  +=  $item->getDiscount();
            }
        }
        return $total;
    }

    public function getTotalPatientAmount() {
        $total    =   0;
        foreach ($this->itemList as $subList) {
            foreach ($subList as $item) {
                $total  +=  $item->getPatientPrice();
            }
        }
        return $total;
    }

	public function addItem($type, $billableItem) {
        array_push($this->itemList[$type], $billableItem);
	}

	public function addItemList($type, $itemList) {
		$resAray    =   array_merge($this->itemList[$type], $itemList);
		$this->itemList[$type]    =   $resAray;
	}

	public function getTotalOriginalAmount() {
		$total    =   0;
		foreach ($this->itemList as $subList) {
            foreach ($subList as $item) {
                $total  +=  $item->getOriginalPrice();
            }
		}
		return $total;
	}

	public function getExaminationType() {
		return $this->examinationType;
	}

	public function getDiscountProgram() {
		return $this->discountProgram;
	}

	/**
	 * Persist payment info of the ticket into database.
	 *
	 */
	public function persist() {
        // Step 1: Persist data of the BillStorage object to pay_payment table
        // Step 2: Update some field of pay_payment table from table ticket (department_id, patient_id);
        // Step 3: Persist data of the billable items to pay_payment_item table
        // Step 4: Update original_price, insurnace_price, discount, and patient_price of pay_payment from data of pay_payment_item table
        $db       =   Celini::dbInstance();
        // Step 1
        $objPayment =   Celini::newORDO('PayPayment', $this->paymentId);
        $objPayment->setDataFromBillStorage($this);
        $objPayment->persist();
        // Step 2
        $sql    =   "UPDATE pay_payment pp
                    INNER JOIN ticket t ON t.ticket_id=pp.ticket_id
                    SET pp.patient_id = t.patient_id, pp.department_id = t.department_id,
                        pp.enum_patient_type = t.enum_patient_type, pp.enum_examination_type = t.enum_examination_type
                    WHERE pp.pay_payment_id='" . $objPayment->get_id() ."'";
        $db->execute($sql);
        // Step 3
        $originalPrice  =   $insurancePrice =   $discount   =   $patientPrice = 0;
        foreach ($this->itemList as $subList) {
        	foreach ($subList as $item) {
        	    if ($item->getItemId() > 0) {
        	       $item->update();
            	}
            	else {
            	    $objPayItem =   Celini::newORDO('PayPaymentItem');
            	    $objPayItem->setDataFromBillableItem($item);
            	    $objPayItem->setPaymentId($objPayment->get_id());
            	    $objPayItem->persist();
            	}
            	$originalPrice  +=   $item->getOriginalPrice();
                $insurancePrice +=   $item->getInsurancePrice();
                $discount       +=   $item->getDiscount();
                $patientPrice   +=   $item->getPatientPrice();
        	}
        }
        // Step 4
        $sql    =   "UPDATE pay_payment pp
                    SET pp.original_price = $originalPrice, pp.insurance_price = $insurancePrice,
                        pp.discount = $discount, pp.patient_price = $patientPrice
                    WHERE pp.pay_payment_id = " . $objPayment->get_id();
        $db->execute($sql);
	}

	public function delete(){
		$objPayment =   Celini::newORDO('PayPayment');
        $objPayment->deletePaymentInfoByTicket($this->ticketId);
	}
	
	public function setPaymentId($id) {
	    $this->paymentId   =   $id;
	}
	
	public function setupPaymentId() {
	    $paymentId =   PayPayment::getPaymentIdByTicket($this->ticketId);
	    $this->paymentId   =   $paymentId;
	}
}
?>